#!/usr/bin/env python
import math

"""
/* radar_math.py - it's a radar UI game based on diversity-daemon 
 *
 * Copyright 2009 
 * Authored by Erin Yueh <erinyueh@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */
"""


def getObjectDegree(x0,y0,x,y):
	# calculate the degree between NEO self and other object
	if(y==y0):
                if(x>x0):
                        return  math.pi*0.5
                elif(x<x0):
                        return math.pi*1.5
                elif(x0==x):
                        return 0

	degree = -(math.atan((x - x0) / (y - y0)))

	if (y > y0):
		degree += math.pi
	elif (degree < 0):
		degree += 2 * math.pi

	return round(degree,3)


def calObjectCoordToGeometry(x,y,w,h,neome_lon, neome_lat,r):
	# calculate UI coordinate to geometry in map 
	
	(center_x, center_y) = (w/2, h/2)
	
	lon = (x - center_x)/w * 2 * r + neome_lon

	if( center_y > y):
		lat = (center_y - y)/h * 2 * r + neome_lat
	else:
		lat = 0 - ((y - center_y)/h * 2 * r + neome_lat)

	return lon,lat


def calObjectGeometryToCoord(lon,lat,w,h,neome_lon, neome_lat,r):
	# calculate the coornidate to present in UI 

	(center_x, center_y) = (w/2, h/2)

	x = center_x + (((lon-neome_lon)/(2*r))*w) 
	y = center_y - (((lat-neome_lat)/(2*r))*h)

	return x,y


def calObjectDistance(x1,y1,x2,y2):
        # calculate the distance between NEO self and other target object
        distance = math.sqrt(((x1-x2) * (x1-x2)) + ((y1-y2) * (y1-y2)))
        #print 'x1,y1,distance,x2,y2',x1,y1,distance,x2,y2
        return distance

def calDistanceinGeometry(d,w,h,r):

         d1 = 2*r / w * d
         
         return d1
